home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu248.dms / pu248.adf / Intuition / Menus / Example4.c < prev    next >
C/C++ Source or Header  |  1992-05-01  |  13KB  |  338 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Intuition               Amiga C Club       */
  7. /* Chapter: Menus                       Tulevagen 22       */
  8. /* File:    Example4.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-01                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program opens a normal window to which we connect a menu strip. */
  21. /* The menu will look like this:                                        */
  22. /*                                                                      */
  23. /* Status                                                               */
  24. /* --------------                                                       */
  25. /* | v Readmode | (ghosted)                                             */
  26. /* |   Editmode |                                                       */
  27. /* --------------                                                       */
  28. /*                                                                      */
  29. /* The Readmode item is selected and ghosted, and when the user selects */
  30. /* the Editmode item, it will become disabled (ghosted) while the read- */
  31. /* mode item will be enabled (not ghosted). This means that if the      */
  32. /* program is in "readmode", the user should only be able to chose the  */
  33. /* "editmode", and v.v.                                                 */ 
  34. /*                                                                      */
  35. /* The purpose with this program is to show how you can use the OnMenu  */
  36. /* and OffMenu functions inorder to make an "user-friendly interface".  */
  37.  
  38.  
  39.  
  40. #include <intuition/intuition.h>
  41.  
  42.  
  43.  
  44. struct IntuitionBase *IntuitionBase;
  45.  
  46.  
  47.  
  48. /*************************************************************************/
  49. /*                       E D I T M O D E   I T E M                       */
  50. /*************************************************************************/
  51.  
  52. /* The text for the editmode item: */
  53. struct IntuiText my_editmode_text=
  54. {
  55.   2,          /* FrontPen, black. */
  56.   0,          /* BackPen, not used since JAM1. */
  57.   JAM1,       /* DrawMode, do not change the background. */
  58.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  59.               /* This will leave enough space for the check mark. */
  60.   1,          /* TopEdge, 1 line down. */
  61.   NULL,       /* TextAttr, default font. */
  62.   "Editmode", /* IText, the string. */
  63.   NULL        /* NextItem, no link to other IntuiText structures. */
  64. };
  65.  
  66. /* The MenuItem structure for the editmode item: */
  67. struct MenuItem my_editmode_item=
  68. {
  69.   NULL,            /* NextItem, last item in the list. */
  70.   0,               /* LeftEdge, 0 pixels out. */
  71.   10,              /* TopEdge, 10 lines down. */
  72.   150,             /* Width, 150 pixels wide. */
  73.   10,              /* Height, 10 lines high. */
  74.   ITEMTEXT|        /* Flags, render this item with text. */
  75.   ITEMENABLED|     /*        this item will be enabled. */
  76.   CHECKIT|         /*        it is an attribute item. */
  77.   HIGHCOMP,        /*        complement the colours when highlihted. */
  78.   0xFFFFFFFD,      /* MutualExclude, mutualexclude all items except the */
  79.                    /*                second (this) one. */
  80.   (APTR) &my_editmode_text, /* ItemFill, pointer to the text. */
  81.   NULL,            /* SelectFill, nothing since we complement the col. */
  82.   0,               /* Command, not accessable from the keyboard. */
  83.   NULL,            /* SubItem, ignored by Intuition. */
  84.   MENUNULL,        /* NextSelect, no items selected. */
  85. };
  86.  
  87.  
  88.  
  89. /*************************************************************************/
  90. /*                       R E A D M O D E   I T E M                       */
  91. /*************************************************************************/
  92.  
  93. /* The text for the readmode item: */
  94. struct IntuiText my_readmode_text=
  95. {
  96.   2,          /* FrontPen, black. */
  97.   0,          /* BackPen, not used since JAM1. */
  98.   JAM1,       /* DrawMode, do not change the background. */
  99.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  100.               /* This will leave enough space for the check mark. */
  101.   1,          /* TopEdge, 1 line down. */
  102.   NULL,       /* TextAttr, default font. */
  103.   "Readmode", /* IText, the string. */
  104.   NULL        /* NextItem, no link to other IntuiText structures. */
  105. };
  106.  
  107. /* The MenuItem structure for the readmode item: */
  108. struct MenuItem my_readmode_item=
  109. {
  110.   &my_editmode_item, /* NextItem, pointer to the second (edit) item. */
  111.   0,                 /* LeftEdge, 0 pixels out. */
  112.   0,                 /* TopEdge, 0 lines down. */
  113.   150,               /* Width, 150 pixels wide. */
  114.   10,                /* Height, 10 lines high. */
  115.   ITEMTEXT|          /* Flags, render this item with text. */
  116.                      /*        this item will be disabled. */
  117.   CHECKIT|           /*        it is an attribute item. */
  118.   CHECKED|           /*        this item is initially selected. */
  119.   HIGHCOMP,          /*        complement the colours when highlihted. */
  120.   0xFFFFFFFE,        /* MutualExclude, mutualexclude all items except the */
  121.                      /*                first (this) one. */
  122.   (APTR) &my_readmode_text, /* ItemFill, pointer to the text. */
  123.   NULL,              /* SelectFill, nothing since we complement the col. */
  124.   0,                 /* Command, not accessable from the keyboard. */
  125.   NULL,              /* SubItem, ignored by Intuition. */
  126.   MENUNULL,          /* NextSelect, no items selected. */
  127. };
  128.  
  129.  
  130.  
  131. /*************************************************************************/
  132. /*                              M E N U                                  */
  133. /*************************************************************************/
  134.  
  135. /* The Menu structure for the first (and only) menu: */
  136. struct Menu my_menu=
  137. {
  138.   NULL,             /* NextMenu, no more menu structures. */
  139.   0,                /* LeftEdge, left corner. */
  140.   0,                /* TopEdge, for the moment ignored by Intuition. */
  141.   50,               /* Width, 50 pixels wide. */
  142.   0,                /* Height, for the moment ignored by Intuition. */
  143.   MENUENABLED,      /* Flags, this menu will be enabled. */
  144.   "Status",         /* MenuName, the string. */
  145.   &my_readmode_item /* FirstItem, pointer to the first item in the list. */
  146. };
  147.  
  148.  
  149.  
  150. /* Declare a pointer to a Window structure: */ 
  151. struct Window *my_window;
  152.  
  153. /* Declare and initialize your NewWindow structure: */
  154. struct NewWindow my_new_window=
  155. {
  156.   50,            /* LeftEdge    x position of the window. */
  157.   25,            /* TopEdge     y positio of the window. */
  158.   250,           /* Width       250 pixels wide. */
  159.   100,           /* Height      100 lines high. */
  160.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  161.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  162.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  163.                  /*             user has selected the Close window gad. */
  164.   MENUPICK,
  165.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  166.   WINDOWCLOSE|   /*             Close Gadget. */
  167.   WINDOWDRAG|    /*             Drag gadget. */
  168.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  169.   WINDOWSIZING|  /*             Sizing Gadget. */
  170.   ACTIVATE,      /*             The window should be Active when opened. */
  171.   NULL,          /* FirstGadget No Custom gadgets. */
  172.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  173.   "Read or Edit", /* Title      Title of the window. */
  174.   NULL,          /* Screen      Connected to the Workbench Screen. */
  175.   NULL,          /* BitMap      No Custom BitMap. */
  176.   80,            /* MinWidth    We will not allow the window to become */
  177.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  178.   300,           /* MaxWidth    than 300 x 200. */
  179.   200,           /* MaxHeight */
  180.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  181. };
  182.  
  183.  
  184.  
  185. main()
  186. {
  187.   /* Boolean variable used for the while loop: */
  188.   BOOL close_me;
  189.  
  190.   /* Declare a variable in which we will store the IDCMP flag: */
  191.   ULONG class;
  192.   
  193.   /* If we recieve a MENUPICK event, the Code field of the message */
  194.   /* structure will contain the menu number of the first selected item. */
  195.   /* Declare a variable to store the Code value in, and two extra menu */
  196.   /* number variables: */
  197.   USHORT code, menu_number, number;
  198.   
  199.   /* Declare a MenuItem pointer: */
  200.   struct MenuItem *item;
  201.   
  202.   /* Declare a pointer to an IntuiMessage structure: */
  203.   struct IntuiMessage *my_message;
  204.  
  205.  
  206.  
  207.   /* Before we can use Intuition we need to open the Intuition Library: */
  208.   IntuitionBase = (struct IntuitionBase *)
  209.     OpenLibrary( "intuition.library", 0 );
  210.   
  211.   if( IntuitionBase == NULL )
  212.     exit(); /* Could NOT open the Intuition Library! */
  213.  
  214.  
  215.  
  216.   /* We will now try to open the window: */
  217.   my_window = (struct Window *) OpenWindow( &my_new_window );
  218.   
  219.   /* Have we opened the window succesfully? */
  220.   if(my_window == NULL)
  221.   {
  222.     /* Could NOT open the Window! */
  223.     
  224.     /* Close the Intuition Library since we have opened it: */
  225.     CloseLibrary( IntuitionBase );
  226.  
  227.     exit();  
  228.   }
  229.  
  230.  
  231.  
  232.   /* We have opened the window, and everything seems to be OK. */
  233.  
  234.  
  235.  
  236.   SetMenuStrip( my_window, &my_menu );
  237.   printf("Menustrip connected to window!\n");
  238.  
  239.  
  240.   close_me = FALSE;
  241.  
  242.   /* Stay in the while loop until the user has selected the Close window */
  243.   /* gadget: */
  244.   while( close_me == FALSE )
  245.   {
  246.     /* Wait until we have recieved a message: */
  247.     Wait( 1 << my_window->UserPort->mp_SigBit );
  248.  
  249.     /* As long as we collect messages sucessfully we stay in the loop: */
  250.     while(my_message=(struct IntuiMessage *) GetMsg( my_window->UserPort ))
  251.     {
  252.       /* After we have collected the message we can read it, and save any */
  253.       /* important values which we maybe want to check later: */
  254.       class = my_message->Class;
  255.       code = my_message->Code;
  256.  
  257.  
  258.       /* After we have read it we reply as fast as possible: */
  259.       /* REMEMBER! Do never try to read a message after you have replied! */
  260.       /* Some other process has maybe changed it. */
  261.       ReplyMsg( my_message );
  262.  
  263.       /* Check which IDCMP flag was sent: */
  264.       if( class == CLOSEWINDOW )
  265.         close_me=TRUE; /* The user selected the Close window gadget! */  
  266.  
  267.       if(class == MENUPICK)
  268.       {
  269.         printf("\nMenu pick!\n");
  270.         menu_number = code;
  271.         
  272.         while( menu_number != MENUNULL )
  273.         {
  274.           /* Get the address of the item: */
  275.           item = (struct MenuItem *) ItemAddress( &my_menu, menu_number );
  276.  
  277.  
  278.  
  279.           /* Check which item was selected: */
  280.           if( item == &my_readmode_item )
  281.           {
  282.             /* The Readmode (first) item was selected! */
  283.             printf("We are now in READMODE!\n");
  284.             
  285.             /* Disable the Readmode item: */
  286.             number = SHIFTMENU( 0 ) + SHIFTITEM( 0 ) + SHIFTSUB( NOSUB );
  287.             /*       first menu       first item       no subitem. */
  288.             OffMenu( my_window, number );
  289.  
  290.             /* Enable the Editmode item: */
  291.             number = SHIFTMENU( 0 ) + SHIFTITEM( 1 ) + SHIFTSUB( NOSUB );
  292.             /*       first menu       second item      no subitem. */
  293.             OnMenu( my_window, number );
  294.           }
  295.  
  296.           if( item == &my_editmode_item )
  297.           {
  298.             /* The Editmode (second) item was selected! */
  299.             printf("We are now in EDITMODE!\n");
  300.             
  301.             /* Disable the Editmode item: */
  302.             number = SHIFTMENU( 0 ) + SHIFTITEM( 1 ) + SHIFTSUB( NOSUB );
  303.             /*       first menu       second item      no subitem. */
  304.             OffMenu( my_window, number );
  305.  
  306.             /* Enable the Readmode item: */
  307.             number = SHIFTMENU( 0 ) + SHIFTITEM( 0 ) + SHIFTSUB( NOSUB );
  308.             /*       first menu       first item       no subitem. */
  309.             OnMenu( my_window, number );
  310.           }
  311.  
  312.  
  313.  
  314.           /* Get the following item's menu number: */
  315.           menu_number = item->NextSelect;
  316.         }
  317.       }
  318.     }
  319.   }
  320.  
  321.  
  322.  
  323.   printf("Menustrip removed from window!\n");
  324.   ClearMenuStrip( my_window );
  325.  
  326.  
  327.  
  328.   /* Close the window: */
  329.   CloseWindow( my_window );
  330.  
  331.  
  332.  
  333.   /* Close the Intuition Library since we have opened it: */
  334.   CloseLibrary( IntuitionBase );
  335.   
  336.   /* THE END */
  337. }
  338.